home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs_src_gs.lha / gs5.03 / zimage.c < prev    next >
C/C++ Source or Header  |  1997-07-18  |  13KB  |  478 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zimage.c */
  20. /* Image operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "estack.h"            /* for image[mask] */
  25. #include "gsstruct.h"
  26. #include "ialloc.h"
  27. #include "igstate.h"
  28. #include "ilevel.h"
  29. #include "store.h"
  30. #include "gscspace.h"
  31. #include "gsmatrix.h"
  32. #include "gsimage.h"
  33. #include "stream.h"
  34. #include "ifilter.h"        /* for stream exception handling */
  35. #include "iimage.h"
  36.  
  37. /* Define the maximum number of image components. */
  38. /* (We refer to this as M in a number of places below.) */
  39. #ifdef DPNEXT
  40. #  define max_components 5
  41. #else
  42. #  define max_components 4
  43. #endif
  44.  
  45. /* Forward references */
  46. private int image_setup(P5(gs_image_t *pim, bool multi, os_ptr op, const gs_color_space *pcs, int npop));
  47. private int image_proc_continue(P1(os_ptr));
  48. private int image_file_continue(P1(os_ptr));
  49. private int image_file_buffered_continue(P1(os_ptr));
  50. private int image_string_process(P3(os_ptr, gs_image_enum *, int));
  51. private int image_cleanup(P1(os_ptr));
  52.  
  53. /* <width> <height> <bits/sample> <matrix> <datasrc> image - */
  54. int
  55. zimage(register os_ptr op)
  56. {    return zimage_opaque_setup(op, false,
  57. #ifdef DPNEXT
  58.                    false,
  59. #endif
  60.                    NULL, 5);
  61. }
  62.  
  63. /* <width> <height> <paint_1s> <matrix> <datasrc> imagemask - */
  64. int
  65. zimagemask(register os_ptr op)
  66. {    gs_image_t image;
  67.  
  68.     check_type(op[-2], t_boolean);
  69.     gs_image_t_init_mask(&image, op[-2].value.boolval);
  70.     return image_setup(&image, false, op, NULL, 5);
  71. }
  72.  
  73. /* Common setup for image, colorimage, and alphaimage. */
  74. /* Fills in MultipleDataSources, BitsPerComponent, HasAlpha. */
  75. #ifdef DPNEXT
  76. int
  77. zimage_opaque_setup(os_ptr op, bool multi, bool has_alpha,
  78.   const gs_color_space *pcs, int npop)
  79. {    gs_image_t image;
  80.  
  81.     check_int_leu(op[-2], (level2_enabled ? 12 : 8));  /* bits/sample */
  82.     gs_image_t_init_color(&image);
  83.     image.BitsPerComponent = (int)op[-2].value.intval;
  84.     image.HasAlpha = has_alpha;
  85.     return image_setup(&image, multi, op, pcs, npop);
  86. }
  87. #else
  88. int
  89. zimage_opaque_setup(os_ptr op, bool multi,
  90.   const gs_color_space *pcs, int npop)
  91. {    gs_image_t image;
  92.  
  93.     check_int_leu(op[-2], (level2_enabled ? 12 : 8));  /* bits/sample */
  94.     gs_image_t_init_color(&image);
  95.     image.BitsPerComponent = (int)op[-2].value.intval;
  96.     return image_setup(&image, multi, op, pcs, npop);
  97. }
  98. #endif
  99.  
  100. /* Common setup for [color]image and imagemask. */
  101. /* Fills in Width, Height, ImageMatrix, ColorSpace. */
  102. private int
  103. image_setup(gs_image_t *pim, bool multi, os_ptr op,
  104.   const gs_color_space *pcs, int npop)
  105. {    int code;
  106.  
  107.     check_type(op[-4], t_integer);    /* width */
  108.     check_type(op[-3], t_integer);    /* height */
  109.     if ( op[-4].value.intval < 0 || op[-3].value.intval < 0 )
  110.       return_error(e_rangecheck);
  111.     if ( (code = read_matrix(op - 1, &pim->ImageMatrix)) < 0 )
  112.       return code;
  113.     pim->ColorSpace = pcs;
  114.     pim->Width = (int)op[-4].value.intval;
  115.     pim->Height = (int)op[-3].value.intval;
  116.     return zimage_setup(pim, multi, op, npop);
  117. }
  118.  
  119. /* Common setup for Level 1 image/imagemask/colorimage, alphaimage, and */
  120. /* the Level 2 dictionary form of image/imagemask. */
  121. int
  122. zimage_setup(const gs_image_t *pim, bool multi, const ref *sources, int npop)
  123. {    int code;
  124.     gs_image_enum *penum;
  125.     int px;
  126.     const ref *pp;
  127.     int num_sources =
  128.       (multi ? gs_color_space_num_components(pim->ColorSpace) : 1);
  129.     bool must_buffer = false;
  130.  
  131.     /*
  132.      * We push the following on the estack.  "Optional" values are
  133.      * set to null if not used, so the offsets will be constant.
  134.      *    Control mark,
  135.      *    M data sources (1-M actually used),
  136.      *    M row buffers (only if must_buffer),
  137.      *    current plane index,
  138.      *    current byte in row (only if must_buffer, otherwise 0),
  139.      *    enumeration structure.
  140.      */
  141. #define inumpush (max_components * 2 + 4)
  142.     check_estack(inumpush + 2);  /* stuff above, + continuation + proc */
  143. #ifdef DPNEXT
  144.     if ( pim->HasAlpha && multi )
  145.       ++num_sources;
  146. #endif
  147.     /*
  148.      * Note that the data sources may be procedures, strings, or (Level
  149.      * 2 only) files.  (The Level 1 reference manual says that Level 1
  150.      * requires procedures, but Adobe Level 1 interpreters also accept
  151.      * strings.)  The sources must all be of the same type.
  152.      *
  153.      * If the sources are files, and two or more are the same file,
  154.      * we must buffer data for each row; otherwise, we can deliver the
  155.      * data directly out of the stream buffers.
  156.      */
  157.     for ( px = 0, pp = sources; px < num_sources; px++, pp++ )
  158.     {    switch ( r_type(pp) )
  159.         {
  160.         case t_file:
  161.             if ( !level2_enabled )
  162.               return_error(e_typecheck);
  163.             /* Check for aliasing. */
  164.             { int pi;
  165.               for ( pi = 0; pi < px; ++pi )
  166.                 if ( sources[pi].value.pfile == pp->value.pfile )
  167.                   must_buffer = true;
  168.             }
  169.             /* falls through */
  170.         case t_string:
  171.             if ( r_type(pp) != r_type(sources) )
  172.               return_error(e_typecheck);
  173.             check_read(*pp);
  174.             break;
  175.         default:
  176.             if ( !r_is_proc(sources) )
  177.               return_error(e_typecheck);
  178.             check_proc(*pp);
  179.         }
  180.     }
  181.     if ( (penum = gs_image_enum_alloc(imemory, "image_setup")) == 0 )
  182.       return_error(e_VMerror);
  183.     code = gs_image_init(penum, pim, multi, igs);
  184.     if ( code != 0 )    /* error, or empty image */
  185.     {    ifree_object(penum, "image_setup");
  186.         if ( code >= 0 )    /* empty image */
  187.           pop(npop);
  188.         return code;
  189.     }
  190.     push_mark_estack(es_other, image_cleanup);
  191.     ++esp;
  192.     for ( px = 0, pp = sources; px < max_components; esp++, px++, pp++ )
  193.       { if ( px < num_sources )
  194.           *esp = *pp;
  195.         else
  196.           make_null(esp);
  197.         make_null(esp + max_components);    /* buffer */
  198.       }
  199.     esp += max_components + 2;
  200.     make_int(esp - 2, 0);        /* current plane */
  201.     make_int(esp - 1, 0);        /* current byte in row */
  202.     make_istruct(esp, 0, penum);
  203.     switch ( r_type(sources) )
  204.       {
  205.       case t_file:
  206.         if ( must_buffer )
  207.           { /* Allocate a buffer for each row. */
  208.             uint size = gs_image_bytes_per_row(penum);
  209.             for ( px = 0; px < num_sources; ++px )
  210.               { byte *sbody = ialloc_string(size, "image_setup");
  211.             if ( sbody == 0 )
  212.               { esp -= inumpush;
  213.                 image_cleanup(osp);
  214.                 return_error(e_VMerror);
  215.               }
  216.             make_string(esp - (max_components + 2) + px,
  217.                     icurrent_space, size, sbody);
  218.               }
  219.             push_op_estack(image_file_buffered_continue);
  220.           }
  221.         else
  222.           { push_op_estack(image_file_continue);
  223.           }
  224.         break;
  225.       case t_string:
  226.         pop(npop);
  227.         return image_string_process(osp, penum, num_sources);
  228.       default:            /* procedure */
  229.         push_op_estack(image_proc_continue);
  230.         *++esp = sources[0];
  231.         break;
  232.       }
  233.     pop(npop);
  234.     return o_push_estack;
  235. }
  236. /* Continuation for procedure data source. */
  237. private int
  238. image_proc_continue(register os_ptr op)
  239. {    gs_image_enum *penum = r_ptr(esp, gs_image_enum);
  240.     uint size, used;
  241.     int code;
  242.     int px;
  243.     const ref *pproc;
  244.  
  245.     if ( !r_has_type_attrs(op, t_string, a_read) )
  246.     {    check_op(1);
  247.         /* Procedure didn't return a (readable) string.  Quit. */
  248.         esp -= inumpush;
  249.         image_cleanup(op);
  250.         return_error(!r_has_type(op, t_string) ? e_typecheck : e_invalidaccess);
  251.     }
  252.     size = r_size(op);
  253.     if ( size == 0 )
  254.       code = 1;
  255.     else
  256.       code = gs_image_next(penum, op->value.bytes, size, &used);
  257.     if ( code )
  258.       {    /* Stop now. */
  259.         esp -= inumpush;
  260.         pop(1);  op = osp;
  261.         image_cleanup(op);
  262.         return (code < 0 ? code : o_pop_estack);
  263.       }
  264.     pop(1);
  265.     px = (int)++(esp[-2].value.intval);
  266.     pproc = esp - (inumpush - 2);
  267.     if ( px == max_components || r_has_type(pproc + px, t_null) )
  268.       esp[-2].value.intval = px = 0;
  269.     push_op_estack(image_proc_continue);
  270.     *++esp = pproc[px];
  271.     return o_push_estack;
  272. }
  273. /* Continue processing data from an image with file data sources */
  274. /* and no file buffering. */
  275. private int
  276. image_file_continue(os_ptr op)
  277. {    gs_image_enum *penum = r_ptr(esp, gs_image_enum);
  278.     const ref *pproc = esp - (inumpush - 2);
  279.  
  280.     for ( ; ; )
  281.       {    uint size = max_uint;
  282.         int code;
  283.         int pn, px;
  284.         const ref *pp;
  285.  
  286.         /*
  287.          * Do a first pass through the files to ensure that they all
  288.          * have data available in their buffers, and compute the min
  289.          * of the available amounts.
  290.          */
  291.  
  292.         for ( pn = 0, pp = pproc;
  293.               pn < max_components && !r_has_type(pp, t_null);
  294.               ++pn, ++pp
  295.             )
  296.           {    stream *s = pp->value.pfile;
  297.             int min_left = sbuf_min_left(s);
  298.             uint avail;
  299.  
  300.             while ( (avail = sbufavailable(s)) <= min_left )
  301.               {    int next = sgetc(s);
  302.  
  303.                 if ( next >= 0 )
  304.                   { sputback(s);
  305.                     if ( s->end_status == EOFC || s->end_status == ERRC )
  306.                       min_left = 0;
  307.                     continue;
  308.                   }
  309.                 switch ( next )
  310.                   {
  311.                   case EOFC:
  312.                     break;        /* with avail = 0 */
  313.                   case INTC:
  314.                   case CALLC:
  315.                     return
  316.                       s_handle_read_exception(next, pp,
  317.                         NULL, 0, image_file_continue);
  318.                   default:
  319.                   /* case ERRC: */
  320.                     return_error(e_ioerror);
  321.                   }
  322.                 break;            /* for EOFC */
  323.               }
  324.             /* Note that in the EOF case, we can get here with */
  325.             /* avail < min_left. */
  326.             if ( avail >= min_left )
  327.               { avail -= min_left;
  328.                 if ( avail < size )
  329.                   size = avail;
  330.               }
  331.             else
  332.               size = 0;
  333.           }
  334.  
  335.         /* Now pass the min of the available buffered data to */
  336.         /* the image processor. */
  337.  
  338.         if ( size == 0 )
  339.           code = 1;
  340.         else
  341.           { int pi;
  342.             uint used;        /* only set for the last plane */
  343.  
  344.             for ( px = 0, pp = pproc, code = 0; px < pn && !code;
  345.               ++px, ++pp
  346.             )
  347.               code = gs_image_next(penum, sbufptr(pp->value.pfile),
  348.                        size, &used);
  349.             /* Now that used has been set, update the streams. */
  350.             for ( pi = 0, pp = pproc; pi < px; ++pi, ++pp )
  351.               sbufskip(pp->value.pfile, used);
  352.           }
  353.         if ( code )
  354.           {    esp -= inumpush;
  355.             image_cleanup(op);
  356.             return (code < 0 ? code : o_pop_estack);
  357.           }
  358.       }
  359. }
  360. /* Continue processing data from an image with file data sources */
  361. /* and file buffering.  This is similar to the procedure case. */
  362. private int
  363. image_file_buffered_continue(os_ptr op)
  364. {    gs_image_enum *penum = r_ptr(esp, gs_image_enum);
  365.     const ref *pproc = esp - (inumpush - 2);
  366.     int px = esp[-2].value.intval;
  367.     int dpos = esp[-1].value.intval;
  368.     uint size = gs_image_bytes_per_row(penum);
  369.     int code = 0;
  370.  
  371.     while ( !code )
  372.       {    const ref *pp;
  373.         uint avail = size;
  374.         uint used;
  375.         int pi;
  376.  
  377.         /* Accumulate data until we have a full set of planes. */
  378.         while ( px < max_components &&
  379.             !r_has_type((pp = pproc + px), t_null)
  380.               )
  381.           { const ref *pb = pp + max_components;
  382.             uint used;
  383.             int status = sgets(pp->value.pfile, pb->value.bytes,
  384.                        size - dpos, &used);
  385.  
  386.             if ( (dpos += used) == size )
  387.               dpos = 0, ++px;
  388.             else switch ( status )
  389.               {
  390.               case EOFC:
  391.             if ( dpos < avail )
  392.               avail = dpos;
  393.             dpos = 0, ++px;
  394.             break;
  395.               case INTC:
  396.               case CALLC:
  397.             /* Call out to read from a procedure-based stream. */
  398.             esp[-2].value.intval = px;
  399.             esp[-1].value.intval = dpos;
  400.             return s_handle_read_exception(status, pp,
  401.                 NULL, 0, image_file_buffered_continue);
  402.               default:
  403.               /*case ERRC:*/
  404.             return_error(e_ioerror);
  405.               }
  406.           }
  407.         /* Pass the data to the image processor. */
  408.         if ( avail == 0 )
  409.           { code = 1;
  410.             break;
  411.           }
  412.         for ( pi = 0, pp = pproc + max_components;
  413.               pi < px && !code;
  414.               ++pi, ++pp
  415.             )
  416.           code = gs_image_next(penum, pp->value.bytes, avail, &used);
  417.         /* Reinitialize for the next row. */
  418.         px = dpos = 0;
  419.       }
  420.     esp -= inumpush;
  421.     image_cleanup(op);
  422.     return (code < 0 ? code : o_pop_estack);
  423. }
  424. /* Process data from an image with string data sources. */
  425. /* This never requires callbacks, so it's simpler. */
  426. private int
  427. image_string_process(os_ptr op, gs_image_enum *penum, int num_sources)
  428. {    int px = 0;
  429.  
  430.     for ( ; ; )
  431.       {    const ref *psrc = esp - (inumpush - 2) + px;
  432.         uint size = r_size(psrc);
  433.         uint used;
  434.         int code;
  435.  
  436.         if ( size == 0 )
  437.           code = 1;
  438.         else
  439.           code = gs_image_next(penum, psrc->value.bytes, size, &used);
  440.         if ( code )
  441.           {    /* Stop now. */
  442.             esp -= inumpush;
  443.             image_cleanup(op);
  444.             return (code < 0 ? code : o_pop_estack);
  445.           }
  446.         if ( ++px == num_sources )
  447.           px = 0;
  448.       }
  449. }
  450. /* Clean up after enumerating an image */
  451. private int
  452. image_cleanup(os_ptr op)
  453. {    gs_image_enum *penum = r_ptr(esp + inumpush, gs_image_enum);
  454.     const ref *pb;
  455.  
  456.     /* Free any row buffers, in LIFO order as usual. */
  457.     for ( pb = esp + (max_components * 2 + 1);
  458.           pb >= esp + (max_components + 2);
  459.           --pb
  460.         )
  461.       if ( r_has_type(pb, t_string) )
  462.         gs_free_string(imemory, pb->value.bytes, r_size(pb),
  463.                "image_cleanup");
  464.     gs_image_cleanup(penum);
  465.     ifree_object(penum, "image_cleanup");
  466.     return 0;
  467. }
  468.  
  469. /* ------ Initialization procedure ------ */
  470.  
  471. BEGIN_OP_DEFS(zimage_op_defs) {
  472.     {"5image", zimage},
  473.     {"5imagemask", zimagemask},
  474.         /* Internal operators */
  475.     {"1%image_proc_continue", image_proc_continue},
  476.     {"0%image_file_continue", image_file_continue},
  477. END_OP_DEFS(0) }
  478.